//
//  SelectCityMgr.m
//  carcareIOS
//
//  Created by wr on 15/8/6.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "SelectCityMgr.h"
#import "NetViolations.h"
#import "BRProvinceModel.h"
#import "BRCityModel.h"
#import "CalendarMgr.h"

@interface SelectCityMgr()

@property (nonatomic , strong) NSArray * provinceArray;

@end

@implementation SelectCityMgr

static SelectCityMgr * shareSelectCityMgr = nil;
+ (SelectCityMgr *) sharedInstance
{
    @synchronized(self)
    {
        if (shareSelectCityMgr == nil) {
            shareSelectCityMgr = [[self alloc] init];
        }
    }
    return shareSelectCityMgr;
}
-(instancetype)init
{
    self = [super init];
    if (self) {
        [self loadData];
    }
    return self;
}
- (void)loadData
{
    //建立文件管理
    NSString * document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString * plistPath = [document stringByAppendingPathComponent:@"BRProvinceData.plist"];
    
    //如果存在则取出数据，如果不存在则创建
    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
        NSDictionary * loadDic = [NSDictionary dictionaryWithContentsOfFile:plistPath];
        
        NSString * dateStr = [loadDic objectForKey:@"dateStr"];
        
        if ([dateStr isEqualToString:CALENDARMGR.currentDay]) {
            self.provinceArray = [NSArray arrayWithArray:[loadDic objectForKey:@"dataArray"]];
            self.configDataArr(self.provinceArray,loadDic[@"keyDic"]);
        }else
        {
            [self loadInterNetData];
        }
    }else
    {
        [self loadInterNetData];
    }

}
- (void)loadInterNetData
{
    [[NetViolations violationCityList]requestShowLoadingAndErrorTipsSuccess:^(id JSON) {
        NSArray * provinceArray = JSON[@"info"][@"list"];
        NSMutableArray * provinceTempArray = [NSMutableArray arrayWithCapacity:provinceArray.count];
        NSMutableArray * fileProvinceTempArray = [NSMutableArray arrayWithCapacity:provinceArray.count];
//        BRKeyModel * keyModel = [[BRKeyModel alloc]initWithDic:JSON[@"info"][@"config"]];
        for (int index = 0; index < provinceArray.count; index ++) {
            
//            BRProvinceModel * provinceModel = [[BRProvinceModel alloc]init];
//            
//            NSArray * smallKeyArr = [NSArray arrayWithArray:[[NSMutableDictionary dictionaryWithDictionary:provinceArray[index][@"params"]] allKeys]];
//            provinceModel.cid = [provinceArray[index][@"cid"]integerValue];
//            provinceModel.name = provinceArray[index][@"name"];
//            provinceModel.apikey = provinceArray[index][@"apikey"];
//            provinceModel.needcap = [provinceArray[index][@"needcap"]integerValue];
//            provinceModel.params = provinceArray[index][@"params"];
//            provinceModel.paramsKeyContents = smallKeyArr;
//            
//            NSArray * cityArray = provinceArray[index][@"subcities"];
//            NSMutableArray * fileCityTempArray = [NSMutableArray arrayWithCapacity:cityArray.count];
//            for (int indexJ = 0; indexJ < cityArray.count; indexJ ++) {
//                NSMutableDictionary * smallCityDic = cityArray[indexJ];
//                [smallCityDic setObject:[[NSMutableDictionary dictionaryWithDictionary:cityArray[index][@"params"]] allKeys] forKey:@"paramsKeyContents"];
//                BRCityModel * cityModel = [[BRCityModel alloc]initWithDic:smallCityDic];
//                [fileCityTempArray addObject:cityModel.modelDic];
//            }
//            provinceModel.subcities = cityArray;
//            [provinceTempArray addObject:provinceModel];
            NSMutableDictionary * smallProvinceDic = [NSMutableDictionary dictionaryWithDictionary:provinceArray[index]];
            [smallProvinceDic setObject:[NSMutableDictionary dictionaryWithDictionary:provinceArray[index][@"params"]] forKey:@"paramsKeyContents"];
            [fileProvinceTempArray addObject:smallProvinceDic];
        }
        self.provinceArray = fileProvinceTempArray;
        
        NSDictionary * keyDic = JSON[@"info"][@"config"];
        NSString * document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString * plistPath = [document stringByAppendingPathComponent:@"BRProvinceData.plist"];
        NSDictionary * saveDic = [NSDictionary dictionaryWithObjectsAndKeys:fileProvinceTempArray,@"dataArray",CALENDARMGR.currentDay,@"dateStr",keyDic,@"keyDic",nil];
        [saveDic writeToFile:plistPath atomically:YES];
        
        self.configDataArr(self.provinceArray,keyDic);
    } failure:^(id JSON) {
        
    }];

}
@end
